import {getEmployeesAPI, deleteEmployeesAPI, addEmployeesAPI, importExcelAPI, updateUserInfoAPI, getRoleListAPI, assignRolesAPI} from "@/api/employeesApi";
import {getUserInfoAPI} from "@/api/userApi";

const actions = {
  /* 发送请求获取员工列表的action */
  async fetchEmployees({commit}, pageParams) {
    const {data} = await getEmployeesAPI(pageParams)
    commit('updateEmployees', data)
  },
  /* 发送请求根据员工ID删除员工的action */
  async deleteEmployees({commit}, id) {
    const {message} = await deleteEmployeesAPI(id)
    return message
  },
  /* 发送请求新增员工的action */
  async addEmployees({commit}, employees) {
    const {message} = await addEmployeesAPI(employees)
    return message
  },
  /* 批量导入员工(excel表格)的action */
  async importExcel({commit}, data) {
    const array = data.map(item => ({...item, formOfEmployment: item.formOfEmployment === '正式' ? 1 : 2}))
    const {message} = await importExcelAPI(array)
    return message
  },
  /* 根据员工ID发送请求获取员工基本信息的action */
  async findUserDetail({commit}, userId) {
    const {data} = await getUserInfoAPI(userId)
    return data
  },
  /* 发送请求修改员工基本信息的action */
  async updateUserInfo({commit}, data) {
    const {message} = await updateUserInfoAPI(data)
    return message
  },
  /* 发送请求获取所有角色列表的action */
  async fetchRoleList({commit}) {
    const {data: role} = await getRoleListAPI()
    return role.rows
  },
  /* 发送请求给员工分配角色的action */
  async assignRoles({commit}, roleParams) {
    const {message} = await assignRolesAPI(roleParams)
    return message
  }
}
const mutations = {
  /* 更新员工列表的mutation */
  updateEmployees(state, employeesInfo) {
    state.employeeInfo = employeesInfo
  },

  /* 修改total的mutation */
  addTotal(state, total) {
    state.employeeInfo.total = total
  }
}
const state = () => {
  return {
    /* 员工信息 */
    employeeInfo: {},
    /*  角色列表 */
    roleList: []
  }
}
const getters = {
  /* 员工列表 */
  employeesList: state => state.employeeInfo.rows || [],
  /* 数据条数 */
  total: state => state.employeeInfo.total || 0
}
export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions
}

